BLACK_TEXT="|cff000000";
NORMAL_TEXT="|r";

function OnLoad(self,mode)
	--print ("Loading GearBuddy frame " .. mode);
	if (mode == "player") then
		self:RegisterEvent("PLAYER_EQUIPMENT_CHANGED");
	end
end

function OnEvent(self, event, ...)
	if (event == "PLAYER_EQUIPMENT_CHANGED") then
		iLvl("player");
	end
end

function iLvl(mode)
	--Our frame is a child of the paperdoll frame and each slot now has an overlay text box to hold the 
	--ilvl of the item in its slot
	--print ("Running iLvl for " .. mode);
	local invSlot; -- 1 to 18
	local itemID;
	local itemSubtype;
	local totaliLvl = 0;
	local avgiLvl = 0;
	local numSlots = 17; -- for calculating the average
	local itemNamesBySlot = {};
	local itemLevelBySlot = {};
	for invSlot=1,18 do
		itemID = GetInventoryItemID(mode,invSlot);
		if itemID then
			local itemName,_,_,itemLevel,_,_,itemSubtype,_,_,_,_ = GetItemInfo(itemID);
			itemNamesBySlot[invSlot] = itemName;
			itemLevelBySlot[invSlot] = itemLevel;
			if (invSlot == 16) then 
				--if Main Hand is two-hander, only divide by 16 (offhand will be zero)
				if (string.match(itemSubtype,"Two") or string.match(itemSubtype,"Stave") or string.match(itemSubtype,"Polearm") or string.match(itemSubtype,"Fishing")) then
					--print ("Found two handed weapon");
					totaliLvl = totaliLvl + itemLevel;
					numSlots = 16;
				else
					totaliLvl = totaliLvl + itemLevel;
				end 	
			elseif (invSlot ~= 4) then
				--Shirt is in slot 4 - skip it for iLvl calculations
				totaliLvl = totaliLvl + itemLevel;
			end
			--print ("Found " .. itemName .. " with ilvl " .. itemLevel .. " in slot " .. invSlot);
		else 
			itemNamesBySlot[invSlot] = "Missing";
                        itemLevelBySlot[invSlot] = 0;
			--print ("Skipping slot " .. invSlot .. " (Nothing there)");
		end
	end
	avgiLvl = math.floor(totaliLvl/numSlots);
	if (mode == "target") then
		InspectHeadSlotiLvlText:SetFormattedText(itemLevelBySlot[1]);
		InspectNeckSlotiLvlText:SetFormattedText(itemLevelBySlot[2]);
                InspectShoulderSlotiLvlText:SetFormattedText(itemLevelBySlot[3]);
                InspectBackSlotiLvlText:SetFormattedText(itemLevelBySlot[15]);
                InspectChestSlotiLvlText:SetFormattedText(itemLevelBySlot[5]);
                InspectWristSlotiLvlText:SetFormattedText(itemLevelBySlot[9]);
                InspectHandsSlotiLvlText:SetFormattedText(itemLevelBySlot[10]);
                InspectWaistSlotiLvlText:SetFormattedText(itemLevelBySlot[6]);
                InspectLegsSlotiLvlText:SetFormattedText(itemLevelBySlot[7]);
                InspectFeetSlotiLvlText:SetFormattedText(itemLevelBySlot[8]);
                InspectFinger0SlotiLvlText:SetFormattedText(itemLevelBySlot[11]);
                InspectFinger1SlotiLvlText:SetFormattedText(itemLevelBySlot[12]);
                InspectTrinket0SlotiLvlText:SetFormattedText(itemLevelBySlot[13]);
                InspectTrinket1SlotiLvlText:SetFormattedText(itemLevelBySlot[14]);
                InspectMainHandSlotiLvlText:SetFormattedText(itemLevelBySlot[16]);
                InspectSecondaryHandSlotiLvlText:SetFormattedText(itemLevelBySlot[17]);
                InspectRangedSlotiLvlText:SetFormattedText(itemLevelBySlot[18]);
	else
		HeadSlotiLvlText:SetFormattedText(itemLevelBySlot[1]);
		NeckSlotiLvlText:SetFormattedText(itemLevelBySlot[2]);
		ShoulderSlotiLvlText:SetFormattedText(itemLevelBySlot[3]);
		BackSlotiLvlText:SetFormattedText(itemLevelBySlot[15]);
		ChestSlotiLvlText:SetFormattedText(itemLevelBySlot[5]);
		WristSlotiLvlText:SetFormattedText(itemLevelBySlot[9]);
		HandsSlotiLvlText:SetFormattedText(itemLevelBySlot[10]);
		WaistSlotiLvlText:SetFormattedText(itemLevelBySlot[6]);
		LegsSlotiLvlText:SetFormattedText(itemLevelBySlot[7]);
		FeetSlotiLvlText:SetFormattedText(itemLevelBySlot[8]);
		Finger0SlotiLvlText:SetFormattedText(itemLevelBySlot[11]);
		Finger1SlotiLvlText:SetFormattedText(itemLevelBySlot[12]);
		Trinket0SlotiLvlText:SetFormattedText(itemLevelBySlot[13]);
		Trinket1SlotiLvlText:SetFormattedText(itemLevelBySlot[14]);
		MainHandSlotiLvlText:SetFormattedText(itemLevelBySlot[16]);
		SecondaryHandSlotiLvlText:SetFormattedText(itemLevelBySlot[17]);
		RangedSlotiLvlText:SetFormattedText(itemLevelBySlot[18]);
		averageiLvlText:SetFormattedText("Average iLvl (equipped): " .. avgiLvl);
	end
end
